package com.plian.system.controller.sys.warning;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.system.annotation.LogOperation;
import com.plian.system.config.Resubmit;
import com.plian.system.dto.sys.warning.WarningMessageConfigDTO;
import com.plian.system.entity.sys.warning.WarningMessageConfig;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.sys.warning.IWarningMessageConfigService;
import com.plian.system.vo.sys.warning.WarningMessageConfigVO;
import com.plian.system.wrapper.sys.warning.WarningMessageConfigWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping(value = "api/warning/messageConfig")
@Api(value = "预警配置", tags = "预警配置接口")
public class WarningMessageConfigController {

    private IWarningMessageConfigService warningMessageConfigService;
    private WarningMessageConfigWrapper warningMessageConfigWrapper;

    @PostMapping(value = "detail")
    @ApiOperation("详情")
    public R<WarningMessageConfigVO> detail(@RequestBody WarningMessageConfig warningMessageConfig){
        return R.data(warningMessageConfigService.detail(warningMessageConfig));
    }

    @PostMapping(value = "list")
    @ApiOperation("列表")
    public ListResult<WarningMessageConfigVO> list(@RequestBody Query query){
        List<SearchEntity> entityList = query.getSearch();
        if (query.getAscs()==null&&query.getDescs()==null){
            query.setDescs("create_time");
        }
        QueryWrapper<WarningMessageConfig> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new WarningMessageConfig()), entityList
        );
        IPage<WarningMessageConfig> pages = warningMessageConfigService.page(Condition.getPage(query), queryWrapper);
        List<WarningMessageConfigVO> systemVOS = warningMessageConfigWrapper.entityToVO(pages.getRecords());
        return new ListResult<>(systemVOS, CommonCode.SUCCESS, Long.valueOf(pages.getTotal()).intValue());
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @Resubmit(delaySeconds = 0)
    @ApiOperation("新增或修改")
    @LogOperation("预警配置-提交")
    public R submit(@Valid @RequestBody WarningMessageConfigVO warningMessageConfigVO) {
        return R.status(warningMessageConfigService.submit(warningMessageConfigVO));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperation("删除")
    @LogOperation("预警配置-删除")
    public R remove(@RequestBody WarningMessageConfigDTO warningMessageConfigDTO) {
        return R.status(warningMessageConfigService.removeAndChild(warningMessageConfigDTO));
    }


    /**
     * 启用
     */
    @PostMapping("/enable/{id}")
    @ApiOperation("启用")
    @LogOperation("预警配置-启用")
    public R enable(@PathVariable Long id) {
        return R.status(warningMessageConfigService.enable(id));
    }

    /**
     * 停用
     */
    @PostMapping("/stop/{id}")
    @ApiOperation("停用")
    @LogOperation("预警配置-停用")
    public R stop(@PathVariable Long id) {
        return R.status(warningMessageConfigService.stop(id));
    }
}
